package com.caigou.modules.gallery.repository

import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.bean.HttpResult
import com.caigou.modules.gallery.bean.GalleryBean
import com.caigou.modules.gallery.bean.GalleryDetailBean
import com.caigou.modules.gallery.network.HttpGalleryService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/26
 */
@ActivityRetainedScoped
class GalleryRepository @Inject constructor() {

    fun getGalleryList(type: Int): Flow<HttpResult<MutableList<GalleryBean>>> {
        return HttpGalleryService.service.getGalleryList(type)
    }

    fun getCollectList(type: Int, userId: String): Flow<HttpResult<MutableList<GalleryBean>>> {
        return HttpGalleryService.service.getCollectList(type, userId)
    }

    fun getPeriodList(year: Int, type: Int = 1): Flow<HttpResult<MutableList<String>>> {
        return HttpGalleryService.service.getPeriodList(year, type)
    }

    fun getGalleryDetail(depotId: Int, period: Int, type: Int, userId: String): Flow<HttpResult<GalleryDetailBean>> {
        return HttpGalleryService.service.getGalleryDetail(depotId, period, type, userId)
    }

    fun getCommentList(page: Int, limit: Int, depotId: Int, dataId: Int, type: Int, sort: Int, currentUserId: String): Flow<HttpResult<CommentBean>> {
        return HttpGalleryService.service.getCommentList(page, limit, depotId, dataId, type, sort, currentUserId)
    }

    fun getCommentDetail(deoptId: Int, detailId: Int, type: Int, replyId: Int, userId: String): Flow<HttpResult<CommentItemBean>> {
        return HttpGalleryService.service.getCommentDetail(deoptId, detailId, type, replyId, userId)
    }

    fun setReply(
        content: String, files: MutableList<File>, senderId: String,
        receiverId: String, depotId: Int, detailId: Int, type: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", depotId.toString())
            .addFormDataPart("detailId", detailId.toString())
            .addFormDataPart("type", type.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpGalleryService.service.setReply(body.build())
    }

    fun setCommentReply(
        content: String, files: MutableList<File>, senderId: String, receiverId: String,
        depotId: Int, detailId: Int, type: Int, parentId: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", depotId.toString())
            .addFormDataPart("detailId", detailId.toString())
            .addFormDataPart("type", type.toString())
            .addFormDataPart("parentId", parentId.toString())

        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpGalleryService.service.setReply(body.build())
    }

    fun collect(type: Int, depotId: Int, userId: String): Flow<HttpResult<String>> {
        val jsonObjects = JSONObject()
        jsonObjects.put("type", type)
        jsonObjects.put("depotId", depotId)
        jsonObjects.put("userId", userId)
        val body = jsonObjects.toString().toRequestBody()
        return HttpGalleryService.service.collect(body)
    }

    fun setDetailLike(depotId: Int, detailId: Int, type: Int, senderId: String): Flow<HttpResult<String>> {
        val jsonObjects = JSONObject()
        jsonObjects.put("dataId", depotId)
        jsonObjects.put("detailId", detailId)
        jsonObjects.put("type", type)
        jsonObjects.put("senderId", senderId)
        val body = jsonObjects.toString().toRequestBody()
        return HttpGalleryService.service.setDetailLike(body)
    }

    fun setCommentLike(type: Int, replyId: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObjects = JSONObject()
        jsonObjects.put("type", type)
        jsonObjects.put("replyId", replyId)
        jsonObjects.put("senderId", senderId)
        jsonObjects.put("receiverId", receiverId)
        val body = jsonObjects.toString().toRequestBody()
        return HttpGalleryService.service.setCommentLike(body)
    }
}